<?php namespace App\Http\Controllers\admin\device;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\Devices;
use App;
use App\Models\Techs;
use App\Models\Domains;
use App\Models\MainBody;
use App\Models\DeviceTech;
use App\Models\DeviceDomain;
use App\Models\DeviceMainBody;
use App\Models\DeviceCases;
use App\Models\DeviceApply;
use App\Models\Projects;
use App\Models\DeviceFeatures;
use App\Models\DeviceFunctions;
use App\Models\Users;

use Validator;
use Redirect;
use Session;




class DeviceController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		if(Session::has('admin')){
		return view("admin.device.index")->withIsDevice(true);
	    }else{
	    	return Redirect::to('admin/dologin');
	    }
	}



	public function updateDevice()
    {

		$rule = array();
		$msg = array();
        $validator = Validator::make(Input::all(),$rule,$msg);
		if($validator->passes()){
    		$did = Input::get("did");
			$model = Devices::where('id',$did);
			if($model->count()){
				// 更新设备
				$data = Input::except('did', 'main_body', 'tech_id', 'domain_id');
				$model->update($data);
				// 更新应用技术
				$tech = Input::get("tech_id");
				$tech_model = DeviceTech::where("device_id", $did);
				if($tech_model->count()){
					$tech_model->update(["device_id" => $did, "cate_id" => $tech]);
				}
				// 更新领域
				$domain_id = Input::get("domain_id");
				$domain_model = DeviceDomain::where("device_id", $did);
				if($domain_model->count()){
					$domain_model->update(["device_id" => $did, "cate_id" => $domain_id]);
				}

				// 更新主体
				$mainbody_id = Input::get("main_body");
				$main_model = DeviceMainBody::where("device_id", $did);
				if($main_model->count()){
					$main_model->delete();
					foreach ($mainbody_id as $key => $value) {
						DeviceMainBody::create(["device_id" => $did, "mb_id" => $value]);
					}
				}
				return $this->json([]);
			}else{
                return App::abort(404);
			}

		}else{
			return;
		}
	}

	public function saveDevice()
	{
		// return Input::all();
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = Input::get("user_id");
			$data = Input::all();
			$imgArr = Input::only("image");

			$this->moveImages($imgArr, env("DEVICE_DIR"), $uid);

			$data['uid'] = $uid;
			$device = Devices::create($data);
			$deviceTech = [
				"cate_id"	=>	Input::get("tech_id"),
				"device_id"	=>	$device->id
			];
			DeviceTech::create($deviceTech);
			$deviceDomain = ["cate_id" => Input::get("domain_id"), "device_id" => $device->id];
			DeviceDomain::create($deviceDomain);

			$function_name = Input::get("function_name");
			$features_name = Input::get("features_name");
			DeviceFunctions::create(["device_id" => $device->id, "content" => $function_name]);
			DeviceFeatures::create(["device_id" => $device->id, "content" => $features_name]);

			$device_main = Input::get("main_body");
			foreach ($device_main as $main)
			{
				DeviceMainBody::create(["mb_id" => $main, "device_id" => $device->id]);
			}
			$cases = Input::get("cases");
			if($cases){
				foreach ($cases as $key => $value) {
					DeviceCases::create(["name" => $value['name'], "content" => $value['content'], "start_time" => $value['start_time'], "end_time" => $value['end_time'], "device_id" => $device->id]);
				}
			}
			$applys = Input::get("applys");

			if($applys){
				foreach ($applys as $key => $value) {
					DeviceApply::create(["device_id" => $device->id, "project_id" => $value['pid']]);
				}
			}
			return $this->json([]);
		}else{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function editDevice()
	{
	    $tech = Techs::all()->toArray();
	    $domain =Domains::where("parent_id",0)->get()->toArray();
	    $mainBody = MainBody::all()->toArray();
		return view("admin.device.edit_device", ["techs"=>$tech,"domain"=>$domain,"mainBody"=>$mainBody])->withIsDevice(true);
	}

	public function addDevice()
	{
		return view("admin.device.add_device")->withIsDevice(true);
	}


	public function detailDevice()
	{
		$did = Input::get("did");
		$model = Devices::with(["deviceDomain.domain", "deviceTech.tech", "mainbody.main"])->where("id", $did);
		if($model->count())
		{
			$data = $model->get()[0]->toArray();
		}
		else
		{
			return App::abort(404);
		}
		// dd($data);
		return view("admin.device.detail_device", ["data" => $data])->withIsDevice(true);
	}

	public function deviceList()
	{
		$current = (int)Input::get("current");
		$rowCount = Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');

		$model = new Devices;
		$count = $model->get()->count();

		if($keywords){
			$device = $model->where("name", "like", "%".$keywords."%")->orWhere("description", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}else{
			$device = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}
		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $device,
			"total" => $count
		);
	}

	public function getDetail()
	{
		$did = (int)Input::get("did");
		$model = Devices::with(['deviceDomain','deviceTech','deviceDomain.domain','mainbody'])->where("id",$did);
		if($model->count()){
			$data=$model->get()[0]->toArray();
			return $this->json($data);
		}else{
			return APP::abort(404);
		}
		return 1;
	}



	public function getDeviceCases()
	{
		$did = Input::get("did");
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getCaseDetail()
	{
		$cid = Input::get("cid");
		$data = DeviceCases::where("id", $cid)->get()[0]->toArray();
		return $this->json(["data" => $data]);
	}

	public function updateCase()
	{
		$cid = Input::get("cid");
		$did = Input::get("device_id");
		$model = DeviceCases::where("id", $cid);
		$data = Input::except("cid", "device_id");
		$model->update($data);
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function addCase()
	{
		$device_id = Input::get("device_id");
		$data = Input::except("cid");
		DeviceCases::create($data);
		$data = DeviceCases::where("device_id", $device_id)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function deleteCases()
	{
		$cid = Input::get("cid");
		$did = Input::get("did");
		$model = DeviceCases::where("id", $cid);
		$model->delete();
		$data = DeviceCases::where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}



	public function saveApply()
	{
		$pid = Input::get("pid");
		$did = Input::get("did");
		DeviceApply::create(["project_id" => $pid, "device_id" => $did]);
		$data = DeviceApply::with(['project'])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function deleteApply()
	{
		$did = Input::get("did");
		$aid = Input::get("aid");
		$model = DeviceApply::where("id", $aid);
		$model->delete();
		$data = DeviceApply::with(["project"])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getDeviceApply()
	{
		$did = Input::get("did");
		$data = DeviceApply::with(["project"])->where("device_id", $did)->get()->toArray();
		return $this->json(["data" => $data]);
	}


	public function getUser()
	{
		$data = Users::with(["devicetech"])->where("role_id", env("DEVICE_TECH"))->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getDeviceAudit(){
  		$pid = Input::get("uid");
  		$state = Input::get("state");
  		$data = Devices::where("id", $did)->toArray();
  		return $data;
  		$data->update(["verify_state" => $state]);
  	}


  	public function audit()
  	{
  		$did = Input::get("did");
  		$state = Input::get("state");
  		$model = Devices::where("id", $did);
  		if($model->count())
  		{
  			$model->update(["verify_state" => $state]);
  			return $this->json([]);
  		}
  	}



	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
